<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddDiaLog">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <tree-table
        :data="tableInfo.shopTypeList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        border
        class="treeTable"
      >
        <!-- 是否有效 -->
        <template slot="isOk"
                  slot-scope="scope"
        >
          <i class="el-icon-success"
             v-if="scope.row.shopTypeStatus === 1"
             style="color:lightgreen "
          ></i>
          <i class="el-icon-error"
             v-else
             style="color:red "
          ></i>
        </template>

        <!-- 等级 -->
        <template slot="grade"
                  slot-scope="scope"
        >
          <el-tag v-if="scope.row.shopTypeGrade === 1">一级</el-tag>
          <el-tag v-else-if="scope.row.shopTypeGrade === 2">二级</el-tag>
          <el-tag v-else>三级</el-tag>
        </template>

        <!-- 操作 -->
        <template slot="opt"
                  slot-scope="scope"
        >
          <el-button type="warning"
                     icon="el-icon-edit"
                     size="mini"
                     @click="handleEditClick(scope.row)"
          >编辑
          </el-button>
          <el-button type="danger"
                     icon="el-icon-delete"
                     size="mini"
                     @click="handleDel(scope.row.shopTypeId)"
          >删除
          </el-button>
        </template>

      </tree-table>

      <!-- 分页 -->
      <el-pagination
        background
        layout="total,prev, pager, next"
        :total="tableInfo.total"
        @current-change="currentChange"
        @prev-click="handlePrevClick"
        @next-click="handleNextClick"
      >
      </el-pagination>
    </el-card>

    <!--  添加商品分类  -->
    <el-dialog
      title="新增商品分类"
      :visible.sync="addDialogVisible"
      width="30%"
      @close="handleAddDialogClose"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称:" prop="shopTypeName">
          <el-input v-model="addForm.shopTypeName"></el-input>
        </el-form-item>

        <el-form-item label="父级分类:">
          <el-cascader
            clearable
            expand-trigger="hover"
            v-model="shopTypeCascader"
            :options="tableInfo.shopTypeList"
            :props="props"
            @change="handleChange"></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAddShopTypeClick">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改商品分类的Dialog -->
    <el-dialog
      title="修改商品分类"
      :visible.sync="updateDialogVisible"
      width="30%"
      @close="handleUpdateDialogClose"
    >
      <el-form
        :model="updateForm"
        :rules="updateFormRules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称:" prop="shopTypeName">
          <el-input v-model="updateForm.shopTypeName"></el-input>
        </el-form-item>

        <el-form-item label="父级分类:">
          <el-cascader
            clearable
            expand-trigger="hover"
            v-model="shopTypeCascader"
            :options="tableInfo.shopTypeList"
            :props="props"
            @change="handleUpdateChange"></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdateShopTypeClick">确 定</el-button>
      </span>
    </el-dialog>

  </div>

</template>

<script>
export default {
  name: "Cate",
  data() {
    return {
      // 查询参数
      queryInfo: {
        type: 3,
        pageNum: '',
        pageSize: ''
      },
      // 商品分类列表相关信息
      tableInfo: {
        // 表单数据
        shopTypeList: [],
        // 数据总数
        total: 0
      },
      columns: [
        {
          label: '分类名称',
          prop: 'shopTypeName',
        },
        {
          label: '是否有效',
          prop: 'shopTypeStatus',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '等级',
          prop: 'shopTypeGrade',
          type: 'template',
          template: 'grade'
        },
        {
          label: '操作',
          prop: '',
          type: 'template',
          template: 'opt'
        }
      ],
      // 新增商品分类打开关闭
      addDialogVisible: false,
      // 新增商品分类表单
      addForm: {
        shopTypeName: '',
        shopTypeParentId: 0,
        shopTypeGrade: 1
      },
      // 新增商品分类表单的验证规则
      addFormRules: {
        shopTypeName: [
          {required: true, message: '请输入商品分类名称！', trigger: 'blur'}
        ]
      },
      // 配置级联选择框
      props: {
        label: 'shopTypeName',
        value: 'shopTypeId',
        children: 'children',
        // 通过 props.checkStrictly = true 来设置父子节点取消选中关联，从而达到选择任意一级选项的目的
        checkStrictly: true
      },
      // 级联的值
      shopTypeCascader: [],
      // 修改商品分类Dialog的展开关闭
      updateDialogVisible: false,
      // 修改商品分类 form
      updateForm: {},
      // 修改商品分类 form 的验证 规则
      updateFormRules: {
        shopTypeName: [
          {required: true, message: '分类名称不能为空', trigger: 'blur'}
        ]
      }
      // ----- 结束 -----
    }
  },
  created() {
    this.getShopTypeList();
  },
  methods: {
    // currentPage 改变时会触发
    currentChange(pageNum) {
      this.queryInfo.pageNum = pageNum;
      this.getShopTypeList();
    },
    // 用户点击上一页按钮改变当前页后触发
    handlePrevClick(pageNum) {
      this.queryInfo.pageNum = pageNum;
      this.getShopTypeList();
    },
    // 用户点击下一页按钮改变当前页后触发
    handleNextClick(pageNum) {
      this.queryInfo.pageNum = pageNum;
      this.getShopTypeList();
    },
    // 获取商品分类列表
    async getShopTypeList() {
      this.queryInfo.pageNum = 1;
      this.queryInfo.pageSize = 5;
      const {data: res} = await this.$http.get(`/shopType/getShopTypeList/tree`, {
        params: this.queryInfo
      });
      if (!res.status) return this.$message.error(res.message);
      this.$message.success(res.message);
      this.tableInfo.shopTypeList = res.data.shopTypeMapListTree;
      this.tableInfo.total = res.data.total;
    },
    // 更新商品分类按钮
    handleEditClick(row) {
      this.updateForm = row;
      if (row.shopTypeGrade !== 1) {
        this.shopTypeCascader = [row.shopTypeParentId];
      }
      this.updateDialogVisible = true;
    },
    // 删除商品分类
    async handleDel(id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该商品分类, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('取消了删除！')
      }
      console.log(id);
      const {data: res} = await this.$http({
        url: '/shopType/delShopType',
        method: 'delete',
        params: {'id': id}
      });

      if (!res.status) return this.$message.error(res.message);
      await this.getShopTypeList();
      this.$message.success(res.message);
    },
    // 点击显示新增商品分类对话框
    showAddDiaLog() {
      this.getParentShopTypeList();
      this.addDialogVisible = true;
    },
    // 获取父级分类的数据列表
    async getParentShopTypeList() {
      this.queryInfo.type = 2;
      const {data: res} = await this.$http.get(`/shopType/getShopTypeList/tree`, {
        params: this.queryInfo
      });
      if (!res.status) return this.$message.error(res.message);
      this.tableInfo.shopTypeList = res.data.shopTypeMapListTree;
    },
    // 级联改变事件
    handleChange() {
      // 如果shopTypeCascader 数据中 的 length 大于 0， 证明选中的父级分类
      // 反之，就说明没有选中任何父级分类
      if (this.shopTypeCascader.length > 0) {
        // 父级分类Id
        this.addForm.shopTypeParentId = this.shopTypeCascader[this.shopTypeCascader.length - 1]
        this.addForm.shopTypeGrade = this.shopTypeCascader.length + 1;
      } else {
        this.addForm.shopTypeParentId = 0;
        this.addForm.shopTypeGrade = 1;
      }
    },
    // 新增分类点击事件
    async handleAddShopTypeClick() {
      const {data: res} = await this.$http.post(`/shopType/addShopType`, this.addForm);
      if (!res.status) return this.$message.error(res.message);
      this.$message.success(res.message);
      await this.getShopTypeList();
      this.handleAddDialogClose();
      this.addDialogVisible = false;
    },
    // 新增商品分类关闭事件
    handleAddDialogClose() {
      this.addForm = {};
      this.shopTypeCascader = [];
    },
    //更新商品分类
    async handleUpdateShopTypeClick() {
      const {data:res} = await this.$http({
        url: `/shopType/updateShopType`,
        method:'put',
        params: this.updateForm
      });

      if (!res.status) return this.$message.error(res.message);
      this.$message.success(res.message);

      this.handleUpdateDialogClose();
      await this.getShopTypeList();
      this.updateDialogVisible = false;
    },
    // 修改商品分类，关闭事件
    handleUpdateDialogClose() {
      this.updateForm = {};
      this.shopTypeCascader = [];
    },
    // 监控 修改商品分类父级分类级联的改变事件
    handleUpdateChange() {
      if (this.shopTypeCascader.length > 0) {
        // 父级分类Id
        this.updateForm.shopTypeParentId = this.shopTypeCascader[this.shopTypeCascader.length - 1]
        this.updateForm.shopTypeGrade = this.shopTypeCascader.length + 1;
      } else {
        this.updateForm.shopTypeParentId = 0;
        this.updateForm.shopTypeGrade = 1;
      }
    }
    // ----- end -----
  }

}
</script>

<style lang="less" scoped>
.treeTable {
  margin-top: 15px;
}

</style>
